<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Models\FangAttr;
use Illuminate\Http\Request;
use OSS\OssClient;
use OSS\Core\OssException;
use lishuo\oss\Manager;
use lishuo\oss\storage\StorageConfig;


class FangAttrController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //all()获取所有的数据
        $data = (new FangAttr())->getFangAttrAll();
        return view('admin.fang_attr.index', compact('data'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //all()获取所有的数据
        $data = (new FangAttr())->getFangAttrAll();
        return view('admin.fang_attr.create',compact('data'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //参数校验
        $params = $request->except(['_token', 'file']);
        $res = FangAttr::create($params);
        //写入成功，跳转到管理员列表页面
        if ($res) {
            return redirect(route("admin.fang_attr.index"));
        } else {
            //写入失败，还在当前页，来一个友好提示
            return redirect(route("admin.fang_attr.index"))->withErrors("添加房源属性失败");
        }
    }

    /**
     * Display the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request)
    {
        $id = $request->get('id');
        $res = FangAttr::destroy($id);
        if($res){
            return redirect(route('admin.fang_attr.index'));
        }else{
            return redirect(route('admin.fang_attr.index'));
        }

    }



    public function ossUploadFile(Request $request)
    {
        $filePath = $request->file('file')->getPathname();

        $accessKeyId = config('oss.AliAppID');
        $accessKeySecret = config('oss.AliAppSC');

        $endpoint = "http://oss-cn-shanghai.aliyuncs.com";

        $bucket = "2008a-gao";

        $ossName = 'fang_attr/' . md5(rand(11111, 99999) . time()) . '.jpg';

        try {
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);

            $ossClient->uploadFile($bucket, $ossName, $filePath);

            $iconPath = config('oss.AliHttp') . $ossName;
            return $this->writeJson(0, ['icon' => $iconPath], 'success');
        } catch (OssException $e) {
            return $this->writeJson(100003, '', 'errpr');
        }
    }


}
